package com.lagou.controller;

import com.lagou.service.CodeService;
import com.lagou.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/user")
public class UserController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Reference
    CodeService codeService;

    @Autowired
    UserService userService;

    @GetMapping("/register/{email}/{password}/{code}")
    public boolean register(@PathVariable String email, @PathVariable String password, @PathVariable String code) {
        int check = codeService.checkCode(email, code);
        if (check != 0){
            String error = null;
            if (check==1){
                error = "错误";
            }else if (check==2) {
                error = "超时";
            }
            logger.error("用户注册验证码校验失败 邮箱 {} 错误类型 {}", email, error);
            return false;
        }
        if (userService.register(email, password)){
            return true;
        }
        return false;
    }

    @GetMapping("/isRegistered/{email}")
    public boolean isRegistered(@PathVariable String email) {
       return userService.isRegistered(email);
    }

    @GetMapping("/login/{email}/{password}")
    public String login(@PathVariable String email, @PathVariable String password) {
        return userService.login(email, password);
    }

    @GetMapping("/info/{token}")
    public String login(@PathVariable String token) {
        return userService.info(token);
    }

}
